<?php

/**
 * @author    		Asim Zeeshan
 * @authorURL		http://www.asim.pk/
 * @description		TwitterTweets is a Twitter API wrapper written in PHP
 * @projectURL		http://code.google.com/u/asim333/
 * @created    		19-Feb-2009 16:43
 * @lastUpdated		28-Feb-2009 21:48
 */

class TwitterTweets {
	private $username 		= "";
	private $timeline_type 	= "";
	private $contents 		= "";
	private $url 			= "";
	public $error 			= "";
	public $cache			= false;
	public $cache_lifetime 	= 0;
	
	public function __construct() {
		$this->setType("xml");
		$this->setCount(1);
		$this->error 			= false;
		$this->written 			= false;
		$this->cache			= true;
		$this->cache_lifetime 	= 10;
	}
	
	public function getUsername() {
		return $this->username;
	}
	
	public function setUsername($user) {
		$this->username = $user;
	}
	
	public function getType() {
		return $this->timeline_type;
	}
	
	public function setType($type) {
		$this->timeline_type = $type;
	}
	
	public function getCount() {
		return $this->count;
	}
	
	public function setCount($count) {
		if ($count > 200)
			$count = 200;

		if ($count < 1)
			$count = 1;
			
		$this->count = $count;
	}
	
	// get friends timelines
	// http://twitter.com/statuses/friends_timeline.format
	public function constructFileName() {
		if ($this->getUsername()=="" || $this->getType()=="") {
			$this->raiseWarning("Please set configuration first");
			return 0;
		} else {
			return 'http://twitter.com/statuses/user_timeline/'.$this->getUsername().'.'.$this->getType()
					.'?count='.$this->getCount();
		}
	}
	
	private function raiseWarning($str) {
		$this->error = true;
		echo "<br /><font color='red'><strong>TwitterTweets Error: </strong></font>".$str;
	}
	
	public function writeFile() {
		$this->fp = fopen($this->localfile, "w");
		fwrite($this->fp, $this->contents); //write contents of url to cache file
		fclose($this->fp);
	}
	
	public function writeUpdatedFile() {
		$this->url = $this->constructFileName();
		if ($this->error == false) {
			$this->contents = file_get_contents($this->url); //fetch contents
			$this->localfile = "tmp/" . urlencode(md5($this->url));
			
			if ($this->cache == true) {
				if (file_exists($this->localfile)) { // if file exists
					if ((time() - filemtime($this->localfile)) > $this->cache_lifetime) { // if file is outdated
						touch($this->localfile); // remove file
						$this->writeFile(); // re-write file
						// set flags
						$this->written = true;
					} else {
						$this->contents = file_get_contents($this->localfile); // load file contents
					}
				} else { // if file does not exist
					$this->writeFile(); // write file :)
					// set flags
					$this->written = true;
				}
			} else {
				if (file_exists($this->localfile))
					touch($this->localfile); // remove file
				
				$this->writeFile(); // write file
			}
		}
	}
	
	public function getFileContents() {
		if ($this->written == false)
			$this->writeUpdatedFile();
		// return contents
		return $this->contents;
	}
	
	public function getLatestTweet() {
		$this->getFileContents();
		$this->xml = new SimpleXMLElement($this->contents);
		return (array)$this->xml->status[0];
	}
	
	public function getTweets($count, $custom_format = false, $show_replies = false) {
		$this->setCount($count);
		
		// declaring variables
		$this->arr = array();
		$this->node_count = 1;
		
		// initialize
		$this->getFileContents();
		
		if ($this->error == false) {
			if ($custom_format==true) {
				// process request
				if ($show_replies == true) {
					// construct
					$this->xml = new SimpleXMLElement($this->contents);
					$this->arr['user']['id'] = (int)$this->xml->status->user->id;
					$this->arr['user']['name'] = (string)$this->xml->status->user->name;
					$this->arr['user']['screen_name'] = (string)$this->xml->status->user->screen_name;
					$this->arr['user']['location'] = (string)$this->xml->status->user->location;
					$this->arr['user']['description'] = (string)$this->xml->status->user->description;
					$this->arr['user']['profile_image_url'] = (string)$this->xml->status->user->profile_image_url;
					$this->arr['user']['url'] = (string)$this->xml->status->user->url;
					$this->arr['user']['protected'] = (bool)$this->xml->status->user->protected;
					$this->arr['user']['followers_count'] = (int)$this->xml->status->user->followers_count;
					foreach($this->xml as $this->xmlNode) {
						if ($this->xmlNode->in_reply_to_status_id=="") {
							$this->arr['status'][$this->node_count]['id'] = (int)$this->xmlNode->id;
							$this->arr['status'][$this->node_count]['text'] = (string)$this->xmlNode->text;
							$this->arr['status'][$this->node_count]['created_at'] = (string)$this->xmlNode->created_at;
							$this->arr['status'][$this->node_count]['favorited'] = (bool)$this->xmlNode->favorited;
							$this->node_count++;
						}
					}
				} else { // if no replies == true
					$this->xml = new SimpleXMLElement($this->contents);
					$this->arr['user']['id'] = (int)$this->xml->status->user->id;
					$this->arr['user']['name'] = (string)$this->xml->status->user->name;
					$this->arr['user']['screen_name'] = (string)$this->xml->status->user->screen_name;
					$this->arr['user']['location'] = (string)$this->xml->status->user->location;
					$this->arr['user']['description'] = (string)$this->xml->status->user->description;
					$this->arr['user']['profile_image_url'] = (string)$this->xml->status->user->profile_image_url;
					$this->arr['user']['url'] = (string)$this->xml->status->user->url;
					$this->arr['user']['protected'] = (bool)$this->xml->status->user->protected;
					$this->arr['user']['followers_count'] = (int)$this->xml->status->user->followers_count;
					foreach($this->xml as $this->xmlNode) {
							$this->arr['status'][$this->node_count]['id'] = (int)$this->xmlNode->id;
							$this->arr['status'][$this->node_count]['text'] = (string)$this->xmlNode->text;
							$this->arr['status'][$this->node_count]['created_at'] = (string)$this->xmlNode->created_at;
							$this->arr['status'][$this->node_count]['favorited'] = (bool)$this->xmlNode->favorited;
							$this->node_count++;
					}
				}
				return $this->arr;
			} else {
				$this->xml = new SimpleXMLElement($this->contents);
				return $this->xml;
			}
		}
	}
}

?>